<?php
/**
 * 公共基础模型
 * @author yupoxiong<i@yufuping.com>
 */

namespace app\common\model;

use think\db\Query;

class Model extends \think\Model
{
    //状态字段 1 正常 0待审核 -1驳回 -2冻结 -3隐藏获取器，使用场景：粉丝的现有的状态由管理员操作等等。
    public const BOOLEAN_TEXT = [0 => '待审核', 1 => '正常', -1 => '驳回', -2 => '冻结', -3 => '隐藏'];

    //状态字段 1 淘宝 2 京东 3拼多多
    public const BOOLEAN_TYPE = [1 => '淘宝', 2 => '京东', 3 => '拼多多'];

    //状态字段 normal 正常 hidden 异常
    public const BOOLEAN_USER_AGENT_TEXT = ['normal' => "正常", 'hidden' => '异常'];

    //1 收 2支
    public const BOOLEAN_USER_MONEY_RECORD_TEXT = ['1'=>"收入", '2'=>"支出"];

    //1 已处理 0 未处理
    public const BOOLEAN_STATE_TEXT = ['1'=>"已处理", '0'=>"未处理"];

    //1 已处理 -1拒绝 0待审核
    public const BOOLEAN_USER_DRAW_TEXT = ['1'=>"已处理",'-1'=>"拒绝", '0'=>"待审核"];

    //1 差价 2 运费
    public const BOOLEAN_ORDER_TEXT = ['1'=>"差价",'2'=>"运费"];

    //1 系统 2 站长
    public const BOOLEAN_IDEN_TEXT = ['1'=>"系统",'2'=>"站长"];

    //0 待处理 1 已处理
    public const BOOLEAN_COND_TEXT = ['0'=>"待处理",'1'=>"已处理"];

    //1 淘宝 2 京东 3 拼多多
    public const BOOLEAN_CATE_TEXT = ['1'=>"淘宝",'2'=>"京东", '3'=>"拼多多"];

    //1 已转款 0 未转款
    public const BOOLEAN_ISCASH_TEXT = ['1'=>"已转款",'0'=>"未转款"];

    //0 未提交 1 进行中 2 已完成
    public const BOOLEAN_STARS_TEXT = ['0'=>"未提交",'1'=>"进行中",'2'=>"已完成"];

    //1 差价 2 运费
    public const BOOLEAN_TY_TEXT = ['0'=>'','1'=>"差价",'2'=>"运费"];

    //是否为软删除
    public $softDelete = true;

    //软删除字段默认值
    protected $defaultSoftDelete = 0;

    //可搜索字段
    protected $searchField = [];

    //可作为条件的字段
    protected $whereField = [];

    //可做为时间范围查询的字段
    protected $timeField = [];

    //禁止删除的数据id
    public $noDeletionId = [];

    /**
     * 查询处理
     * @var Query $query
     * @var array $param
     */
    public function scopeWhere($query, $param): void
    {
        //关键词like搜索
        $keywords = $param['_keywords'] ?? '';
        if ('' !== $keywords && count($this->searchField) > 0) {
            $this->searchField = implode('|', $this->searchField);
            $query->where($this->searchField, 'like', '%' . $keywords . '%');
        }

        //字段条件查询
        if (count($this->whereField) > 0 && count($param) > 0) {
            foreach ($param as $key => $value) {
                if ($value !== '' && in_array((string)$key, $this->whereField, true)) {
                    $query->where($key, $value);
                }
            }
        }

        //时间范围查询
        if (count($this->timeField) > 0 && count($param) > 0) {
            foreach ($param as $key => $value) {
                if ($value !== '' && in_array((string)$key, $this->timeField, true)) {
                    $field_type = $this->getFieldsType($this->table, $key);
                    $time_range = explode(' - ', $value);
                    [$start_time,$end_time] = $time_range;
                    //如果是int，进行转换
                    if (false !== strpos($field_type, 'int')) {
                        $start_time = strtotime($start_time);
                        if (strlen($end_time) === 10) {
                            $end_time .= '23:59:59';
                        }
                        $end_time = strtotime($end_time);
                    }
                    $query->where($key, 'between', [$start_time, $end_time]);
                }
            }
        }

        //排序
        $order = $param['_order'] ?? '';
        $by    = $param['_by'] ?? 'desc';
        $query->order($order ?: 'id', $by ?: 'desc');
    }

    //状态获取器
    public function getStatusTextAttr($value, $data)
    {
        return self::BOOLEAN_TEXT[$data['status']];
    }

    //状态获取器
    public function getTypeTextAttr($value, $data)
    {
        return self::BOOLEAN_TYPE[$data['type']];
    }

    //状态获取器
    public function getSTateTextAttr($value, $data)
    {
        return self::BOOLEAN_STATE_TEXT[$data['type']];
    }

    //状态获取器
    public function getOrderTextAttr($value, $data)
    {
        return self::BOOLEAN_ORDER_TEXT[$data['type']];
    }

    //状态获取器
    public function getIdenTextAttr($value, $data)
    {
        return self::BOOLEAN_IDEN_TEXT[$data['source']];
    }

    //状态获取器
    public function getCondTextAttr($value, $data)
    {
        return self::BOOLEAN_COND_TEXT[$data['status']];
    }

    //状态获取器
    public function getCateTextAttr($value, $data)
    {
        return self::BOOLEAN_CATE_TEXT[$data['type']];
    }

    //状态获取器
    public function getIsCashTextAttr($value, $data)
    {
        return self::BOOLEAN_ISCASH_TEXT[$data['is_cash']];
    }

    //状态获取器
    public function getStarsTextAttr($value, $data)
    {
        return self::BOOLEAN_STARS_TEXT[$data['status']];
    }

    //状态获取器
    public function getTyTextAttr($value, $data)
    {
        return self::BOOLEAN_TY_TEXT[$data['type_id']];
    }

}